package Factors;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import static java.lang.Math.sqrt;

/**
 * Created by Nishi on 26/12/13.
 * Generate a list of integers such that the numbers are the factors of N
 * and are arranged in increasing numerical order
 */

public class PrimeFactor {

    public static void main(String[] args) {

        System.out.println("Prime factors");

        ArrayList<Integer> primeFactors=generate(17);

        Collections.sort(primeFactors);

        for(int factor:primeFactors){
            System.out.println(factor);
        }


    }

    private static ArrayList<Integer> generate(int number){

        ArrayList<Integer> factorsList = new ArrayList<Integer>();

        // Find the occurrence smallest factor 2 --the even factor  check
        while( number%2==0){
            factorsList.add(2);
            number=number/2;
        }

        // Find the Odd factor up to the squire root of the number
        for ( int i = 3 ; i<=sqrt(number); i+=2){
            while(number%i==0){
                factorsList.add(i);
                number = number/i;

            }
        }

        // if the given number is a prime number greater than 2 ( eg:17)
        if (number > 2){
            factorsList.add(number);
        }
        return factorsList;

    }
}
